<?php
use yii\bootstrap\ActiveForm;
use common\helps\tools;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/bootstrap-select/css/bootstrap-select.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    label{
        text-align: right;
    }
    .modal-body>form>div.col-md-12 { padding-bottom: 20px }
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <div class="modal-body">
                        <?php $form = ActiveForm::begin([
                            'id' => 'add_materiel_form',
                            'action' => ['/materiel/add-materiel']
                        ]); ?>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">* </span>商品分类</label>
                            <div class="col-sm-3">
                                <select id="bigCategory" name="bigCategory" class="form-control">
                                    <option value="">选择一级分类</option>
                                    <?php foreach($bigCategory as $value){?>
                                        <option value="<?= $value['id']?>" <?= ($materiel&&$materiel['bigCategory']==$value['id'])?'selected':'' ?>><?= $value['category_name']?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="col-sm-3">
                                <select id="midCategory" name="midCategory" class="form-control">
                                    <option value="">选择二级分类</option>
                                </select>
                            </div>
                            <div class="col-sm-3">
                                <select id="smallCategory" name="smallCategory" class="form-control">
                                    <option value="">选择三级分类</option>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>仓库</label>
                            <div class="col-sm-4">
                                <select id="warehouseId" name="warehouseId" class="form-control" <?=$materiel?'disabled':''?>>
                                    <option value="">选择仓库</option>
                                    <?php foreach($warehouse as $value){?>
                                        <option value="<?= $value['id']?>" <?= ($materiel&&$materiel['warehouseId']==$value['id'])?'selected':'' ?>><?= $value['warehouseName']?></option>
                                    <?php }?>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>供应商</label>
                            <div class="col-sm-4">
                                <select id="supplierId" class="selectpicker" multiple="multiple" title="请选择供应商">
                                    <?php foreach($supplier as $value){ ?>
                                        <option value="<?= $value['id']?>" <?= ($materiel&&$materiel['supplierId']==$value['id'])?'selected':'' ?>><?= $value['supplierName']?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>物料名称</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="materielName" id="materielName" value="<?= $materiel?$materiel['materielName']:'' ?>" <?=$materiel?'readonly':''?>>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>物料编号</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="materielCode" id="materielCode" value="<?= $materiel?$materiel['materielCode']:'' ?>">
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>品牌</label>
                            <div class="col-sm-4">
                                <select id="brandId" name="brandId" class="form-control">
                                    <option value="">选择品牌</option>
                                    <?php foreach($brand as $value){?>
                                        <option value="<?= $value['id']?>" <?= ($materiel&&$materiel['brandId']==$value['id'])?'selected':'' ?>><?= $value['brand_name']?></option>
                                    <?php }?>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>存储类型</label>
                            <div class="col-sm-4">
                                <select id="storeType" name="storeType" class="form-control">
                                    <option value="">选择存储类型</option>
                                    <?php foreach($storeType as $value){?>
                                        <option value="<?= $value['config_key']?>" <?= ($materiel&&$materiel['storeType']==$value['config_key'])?'selected':'' ?>><?= $value['config_value']?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>订货规格描述</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="specDesc" id="specDesc" value="<?= $materiel?$materiel['specDesc']:'' ?>">
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>订货包装方式</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="packDesc" id="packDesc" value="<?= $materiel?$materiel['packDesc']:'' ?>">
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>采购单位</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="purchaseUnit" id="purchaseUnit" value="<?= $materiel?$materiel['purchaseUnit']:'' ?>" <?=$materiel?'readonly':''?>>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>订货单位</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="orderUnit" id="orderUnit" value="<?= $materiel?$materiel['orderUnit']:'' ?>" <?=$materiel?'readonly':''?>>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>订货单位/采购单位</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="convertUnit" id="convertUnit" value="<?= $materiel?$materiel['convertUnit']:''?>" <?=$materiel?'readonly':''?>>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>订货单位/最小使用单位</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="useUnit" id="useUnit" value="<?= $materiel?$materiel['useUnit']:'' ?>" <?=$materiel?'readonly':''?>>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>最小使用单位说明</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="useDesc" id="useDesc" value="<?= $materiel?$materiel['useDesc']:'' ?>">
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>税率</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="taxRate" id="taxRate" value="<?= $materiel?$materiel['taxRate']:'' ?>" >
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>质保天数</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="safeDay" id="safeDay" value="<?= $materiel?$materiel['safeDay']:'' ?>">
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>采购周期</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="purchaseDay" id="purchaseDay" value="<?= $materiel?$materiel['purchaseDay']:'' ?>">
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>参考成本价</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="referPrice" id="referPrice" value="<?= $materiel?$materiel['referPrice']:'' ?>">
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>管理库存</label>
                            <div class="col-sm-4">
                                <select id="storeFlg" name="storeFlg" class="form-control" <?=$materiel?'disabled':''?>>
                                    <option value="1" <?=($materiel&&$materiel['storeFlg']=='1')?'selected':'' ?>>是</option>
                                    <option value="0" <?=($materiel&&$materiel['storeFlg']=='0')?'selected':'' ?>>否</option>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>销售状态</label>
                            <div class="col-sm-4">
                                <select id="saleFlg" name="saleFlg" class="form-control">
                                    <option value="1" <?=($materiel&&$materiel['saleFlg']=='1')?'selected':'' ?>>上架</option>
                                    <option value="0" <?=($materiel&&$materiel['saleFlg']=='0')?'selected':'' ?>>下架</option>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>启用禁用</label>
                            <div class="col-sm-4">
                                <select id="useFlg" name="useFlg" class="form-control">
                                    <option value="1" <?=($materiel&&$materiel['useFlg']=='1')?'selected':'' ?>>启用</option>
                                    <option value="0" <?=($materiel&&$materiel['useFlg']=='0')?'selected':'' ?>>禁用</option>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>安全库存</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="safeStore" id="safeStore" value="<?= $materiel?$materiel['safeStore']:'' ?>">
                            </div>
                            <label class="col-sm-2 control-label">排序</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="sort" id="sort" value="<?= $materiel?$materiel['sort']:'' ?>">
                            </div>
                        </div>

                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>上传图片</label>
                            <div class="col-sm-4">
                                <button type="button" class="btn btn-success depart-modal upload-img-btn-main-pic" style="width: 120px;">上传商品列表图</button>
                                <button style="<?=($materiel&&$materiel['img'])? '':'display: none' ?>;" data-img="<?=($materiel&&$materiel['img'])?$materiel['img']:'' ?>" type="button" class="btn btn-success depart-modal show-img-btn-main-pic" style="width: 110px;">查看图片</button>
                                <input name="goods_main_pic" value="<?= $materiel?$materiel['img']:'' ?>" id="add_goods_main_pic" type="hidden">

                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger"></span>指定门店</label>
                            <div class="col-sm-4">
                                <select id="shopGroupId" class="selectpicker form-control" title="全部门店" multiple="multiple">
                                    <?php foreach($shopGroup as $value) { ?>
                                        <option value="<?= $value['id']?>"><?= $value['name']?></option>
                                    <?php }?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>生效时间</label>
                            <div class="col-sm-4">
                                <input class="form-control" id="datetime" name="datetime" type="text">
                            </div>
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>供货价</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="salePrice" id="salePrice" value="<?= $materiel?$materiel['salePrice']:'0' ?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="col-sm-2 control-label">商品介绍</label>
                            <div class="col-sm-4">
                                <textarea class="form-control" name="description" id="description"><?= $materiel?$materiel['description']:'' ?></textarea>
                            </div>
                        </div>
                        <input name="id" id="id" type="hidden" value="<?= $materiel?$materiel['id']:'' ?>">
                        <?php ActiveForm::end(); ?>
                    </div>
                    <div style="display: none">
                        <?php $form = ActiveForm::begin(['id'=>'upload_goods_main_pic','action'=>'/common/img-upload'],['options' => ['enctype' => 'multipart/form-data']]) ?>
                        <?= $form->field($model, 'img_url')->fileInput(['class' => 'upload_file_goods_main_pic']) ?>
                        <?php ActiveForm::end() ?>
                    </div>
                    <div class="modal-footer" style="border: 0">
                        <button type="button" class="btn btn-default" onclick="window.close()">关闭
                        </button>
                        <a type="button" class="btn btn-primary ajax_post_add_materiel"> 提交 </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<input id="old_id" value="" type="hidden"/>
<input name="class_len" id="class_len" value="0" type="hidden"/>
<input name="class_len_2" id="class_len_2" value="0" type="hidden"/>
<input type="hidden" name="imageBathUrl" id="imageBathUrl" value="<?php echo Yii::$app->params['imageBathUrl'] . '/' ?>">
<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<script type="text/javascript" src="/static/plugins/xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="/static/plugins/xheditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="../static/plugins/jquery.form.js"></script>
<script type="text/javascript" src="../static/plugins/layer/layer.js"></script>
<script type="text/javascript" src="../static/app/goods-keyword/js/jquery.dropdown.js"></script>
<script type="text/javascript" src="../static/app/goods-keyword/js/mock.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/bootstrap-select/js/bootstrap-select.js"></script>
<script type="text/javascript" src="../static/plugins/bootstrap-select/js/defaults-zh_CN.js"></script>
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script type="text/javascript" src="../static/app/materiel/detail.js?version=<?=Yii::$app->params['version']?>"></script>

<script>
    var midCategory = <?=(is_array($midCategory) ? json_encode($midCategory) : '')?>;
    var smallCategory = <?=(is_array($smallCategory) ? json_encode($smallCategory) : '')?>;
    <?php if ($materiel) { ?>
    $(function () {
        chanageBig(<?=$materiel['bigCategory']?>, <?=$materiel['midCategory']?>);
        chanageMid(<?=$materiel['midCategory']?>, <?=$materiel['smallCategory']?>);
        $("#supplierId").selectpicker('val', <?= json_encode(explode(',',$materiel['supplierId'])) ?>);
        $("#shopGroupId").selectpicker('val', <?= json_encode(explode(',',$materiel['shopGroupId'])) ?>);


        $('#datetime').daterangepicker({
            language: 'zh-CN',
            timePicker: false,
            timePickerIncrement: 30,
            format: 'YYYY-MM-DD',
            locale: {
                format: 'YYYY-MM-DD',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            },
            startDate:'<?=date('Y-m-d', strtotime($materiel['startTime']))?>',
            endDate:'<?=date('Y-m-d', strtotime($materiel['endTime']))?>',
        });

    });
    <?php } else { ?>

    $('#datetime').daterangepicker({
        language: 'zh-CN',
        timePicker: false,
        timePickerIncrement: 30,
        format: 'YYYY-MM-DD',
        locale: {
            format: 'YYYY-MM-DD',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        },
    });
    $('#datetime').val('');


    <?php } ?>


    $('.upload-img-btn-main-pic').click(function(){
        $('.upload_file_goods_main_pic').trigger('click');
    })

    $('.upload_file_goods_main_pic').change(function(){
        if(this.value){
            $('#upload_goods_main_pic').submit();
        }
    });

    $('#upload_goods_main_pic').submit(function(e){
        e.preventDefault();
        $(this).ajaxSubmit({
            beforeSend:function(){
                /*$('.upload-img-btn').attr('disabled',true);*/
            },
            success:function(data){
                var result = $.parseJSON(data);
                if(result.code == 500){
                    $.jBox.error(result.val, '错误');
                }else{
                    $('#add_goods_main_pic').val(result.val);
                    $('.show-img-btn-main-pic').attr('data-img',result.val);
                    $('.show-img-btn-main-pic').show();

                    $('#upload_goods_main_pic').find('input[type=file]').closest('form').get(0).reset();
                }
            }
        });

    });

    $('.show-img-btn-main-pic').click(function(){
        var img_url = $(this).attr('data-img');
        showImg(img_url);
    })

    function showImg(objKey){
        if(objKey == '' || objKey == null || objKey == undefined){
            $.jBox.error('数据为空', '错误');
            return false;
        }
        var json = {
            "title": "分类图片", //相册标题
            "id": 0, //相册id
            "start": 0, //初始显示的图片序号，默认0
            "data": [   //相册包含的图片，数组格式
                {
                    "alt": "",//图片名
                    "pid": 0, //图片id
                    "src": $('#imageBathUrl').val() + objKey, //原图地址
                    "thumb": "" //缩略图地址
                }
            ]
        };
        layer.photos({
            photos: json,
            area:['700px'],
            shift: 1,
            closeBtn:1,
            offset:'100px',
            shadeClose:true
        });
    }

    $("#add_brief_cn , #add_brief_other").xheditor({
        upImgUrl: '/common/xhedit-upload',
        upImgExt: "jpg,jpeg,gif,bmp,png",
        onUpload:insertUpload
    },true);

    function insertUpload(arrMsg)
    {
        var i,msg;
        for(i=0;i<arrMsg.length;i++)
        {
            msg=arrMsg[i];
            $("#xheImgUrl").html(msg);
        }
    }

</script>